﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Data;
using ImageUploaderComponent.Data;

namespace ImageUploaderComponent {
    public enum MediaType{Image,Demo}
    public partial class UploaderControl {
        //        private readonly Uploader uploader;
        private UploaderViewModel viewModel;
        private IDataGridView myDataGridView;
        public ImageDataViewModel CurrentImage { get; set; }
        private bool permissionWarningShown = false;


        public UploaderControl() {

            InitializeComponent();

            ButtonDelete.IsEnabled = false;
            myDataGridView = dataGridView;

            Loaded += mainPageLoaded;
            SetBinding(DataContextWatcherProperty, new Binding());
            myDataGridView.UploadingRowLoaded += myDataGridView_UploadingRowLoaded;
            myDataGridView.SelectionChanged += myDataGridViewSelectionChanged;
            ButtonEdit.IsEnabled = false;
            LayoutCanvas.Height = Application.Current.Host.Content.ActualHeight;
            LayoutCanvas.Width = Application.Current.Host.Content.ActualWidth;
            LayoutCanvas.SizeChanged += layoutCanvasSizeChanged;
            BaseLayoutRoot.SizeChanged += new SizeChangedEventHandler(BaseLayoutRoot_SizeChanged);

        }

        void BaseLayoutRoot_SizeChanged(object sender, SizeChangedEventArgs e) {
            Grid root = sender as Grid;
            LayoutCanvas.Height = Application.Current.Host.Content.ActualHeight;
            LayoutCanvas.Width = Application.Current.Host.Content.ActualWidth;
        }

        void myDataGridView_UploadingRowLoaded(DataGridRow row) {
            viewModel.UploadingRowRecieved(row);
        }

        void layoutCanvasSizeChanged(object sender, SizeChangedEventArgs e) {
            LayoutCanvas.Height = Application.Current.Host.Content.ActualHeight;
            LayoutCanvas.Width = Application.Current.Host.Content.ActualWidth;
            RealLayoutRoot.Height = Application.Current.Host.Content.ActualHeight;
            RealLayoutRoot.Width = Application.Current.Host.Content.ActualWidth;
        }

        void myDataGridViewSelectionChanged(object sender, ImageDataViewModel data) {
            ButtonEdit.IsEnabled = true;
            ButtonDelete.IsEnabled = true;
            if (data.IsDeletable) {
                ButtonDelete.Content = "Delete";
            }
            else {
                ButtonDelete.Content = "Archive";
            }
            CurrentImage = data;
        }
        public static readonly DependencyProperty DataContextWatcherProperty =
            DependencyProperty.Register("DataContextWatcher",
                                typeof(Object), typeof(UploaderControl),
                                new PropertyMetadata(dataContextChanged));

        private static void dataContextChanged(object sender,
                                               DependencyPropertyChangedEventArgs e) {
            var control = (UploaderControl)sender;
            UploaderViewModel model = e.NewValue as UploaderViewModel;
            control.viewModel = model;
            control.subscribePropertyChanged();

        }

        private void subscribePropertyChanged() {
            viewModel.PropertyChanged += viewModel_PropertyChanged;
        }

        void viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) {
            if (e.PropertyName == "IsUploading") {
                if (viewModel.IsUploading == true) {
                    myDataGridView.ShowProgressColumn();
                }
                else {
                    myDataGridView.HideProgressColumn();
                }
            }
        }


        void mainPageLoaded(object sender, RoutedEventArgs e) {
            AnimateExpandMainForm.Begin();
        }

        [ScriptableMember]
        public void Start() {
            Visibility = Visibility.Visible;

        }



        private void buttonUploadClick(object sender, RoutedEventArgs e) {
            var dlg = viewModel.GetDialog();
            bool? retval = dlg.ShowDialog();

            if (retval != null && retval == true) // open/uploaded
            {
                viewModel.attemptUpload(dlg.Files);
            }

        }


        private void listBoxServerFiles_Drop(object sender, DragEventArgs e) {
            FileInfo[] files = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
            viewModel.attemptUpload(files);

        }



        private void showMessageBox(string message) {
            MessageBox.Show(message);
        }


        public void SetStatusText(string status) {
            StatusText.Text = status;
        }

        public ProgressBar ProgressBar1 {
            get { return progressBar1; }
        }



        private void ButtonDelete_Click(object sender, RoutedEventArgs e) {

            viewModel.DeleteOrArchive(myDataGridView.SelectedItem);
            BindGrid(true);
        }




        private void ButtonEdit_Click(object sender, RoutedEventArgs e) {
            ImageEditor.FinishedCallback = BindGrid;
            ImageEditor.Visibility = Visibility.Visible;
            ImageEditor.DataContext = CurrentImage;

        }

        public void BindGrid(bool ignore) {
            //myDataGridView.DataContext = viewModel;
            //myDataGridView.ItemsSource = viewModel;
        }


        private void buttonDoneClick(object sender, RoutedEventArgs e) {
            viewModel.SaveState();
            this.Visibility = Visibility.Collapsed;
            hideSilverlightWindow();
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e) {
            if (myDataGridView == null) return;
            RadioButton button = (RadioButton) sender;
            if(button == RadioButtonDemos){
                myDataGridView.Filter(MediaType.Demo);
                return;
            }
            myDataGridView.Filter(MediaType.Image);
        }

        private void hideSilverlightWindow() {
            HtmlPage.Window.Invoke("destroySilverlightWindow", null);
        }
    }
}
